package com.mengyan.manage.cms.controller;

import com.mengyan.api.cms.CmsSiteControllerApi;
import com.mengyan.api.cms.CmsTemplateControllerApi;
import com.mengyan.framework.domain.cms.CmsTemplate;
import com.mengyan.framework.domain.cms.request.QueryTemplateRequest;
import com.mengyan.framework.model.response.QueryResponseResult;
import com.mengyan.framework.model.response.ResponseResultBase;
import com.mengyan.manage.cms.service.SiteService;
import com.mengyan.manage.cms.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/cms/template")
public class CmsTemplateController implements CmsTemplateControllerApi {

    private final TemplateService templateService;

    @Autowired
    public CmsTemplateController(TemplateService templateService) {
        this.templateService = templateService;
    }

    @Override
    @GetMapping("/list")
    public QueryResponseResult findAll() {

        return templateService.findAll();
    }

    @Override
    @GetMapping("/list/{page}/{size}")
    public QueryResponseResult findList(int page, int size, QueryTemplateRequest queryTemplateRequest) {
        return templateService.findList(page,size,queryTemplateRequest);
    }

    @Override
    @RequestMapping("/save")
    public ResponseEntity<String> save(@RequestParam("multipartfiles") MultipartFile[] multipartfiles) throws IOException {

        String id = templateService.save(multipartfiles);
        HttpStatus status = id != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
        return new ResponseEntity<String>(id, status);
    }

    @Override
    @PostMapping("/add")
    public ResponseResultBase<CmsTemplate> add(@RequestBody CmsTemplate cmsTemplate) {
        return templateService.add(cmsTemplate);
    }
}
